package com.by.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.by.entity.Userinfo;
import com.by.service.UserinfoService;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 * (Userinfo)表控制层
 *
 * @author 白也
 * @since 2024-01-17 16:17:11
 */
@Controller
@RequestMapping(value = "userinfo")
public class UserinfoController {

    /**
     * 服务对象
     */
    @Resource
    private UserinfoService userinfoService;

    @Autowired
    private DefaultKaptcha defaultKaptcha;

    @GetMapping("/code")
    public void captcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
        // 设置响应头，防止浏览器缓存
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        // 生成验证码
        String captchaText = defaultKaptcha.createText();
        // 将验证码保存到session中，用于后续验证
        HttpSession session = request.getSession();
        session.setAttribute("captcha", captchaText);
        // 生成验证码图片并输出
        BufferedImage image = defaultKaptcha.createImage(captchaText);
        ImageIO.write(image, "jpeg", response.getOutputStream());
    }

    /**
     * 用户登录
     * @param username 用户名
     * @param password 密码
     * @param request request
     * @param code 验证码
     * @return 用户信息
     */
    @GetMapping(value = "/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("code") String code,
                        HttpServletRequest request,
                        Model model) {
        //校验之前需要校验验证码
        String captcha = (String)request.getSession().getAttribute("captcha");
        System.out.println(captcha.equals(code));
        if (captcha.equals(code)){
            //校验用户名和密码
            Userinfo login = userinfoService.login(username, password);
            //获取sessionID
            if (login != null) {
                String newSessionID =  request.getSession().getId()+username;
                System.out.println("newSessionID = " + newSessionID);
                request.setAttribute("user",newSessionID);
                return "index";
            } else {
                model.addAttribute("message","密码错误");
                return "error";
            }
        }else {
            model.addAttribute("message","验证码错误");
            return "error";
        }
    }


    @PostMapping
    public String register(@RequestParam("username") String username,
                           @RequestParam("password") String password,
                           @RequestParam("email") String email,
                           Model model){
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername(username);
        userinfo.setPassword(password);
        userinfo.setEmail(email);
        if (userinfoService.register(userinfo)) {
            return "login";
        }else {
            model.addAttribute("message","注册失败");
            return "error";
        }
    }


    @PostMapping("updatePass")
    public String updatePass(@RequestParam("username") String username,
                             @RequestParam("email") String email,
                             @RequestParam("password") String password,
                             Model model){
        if (userinfoService.findPass(username,email)) {
            //校验成功
            if (userinfoService.updatePassWord(password,username)) {
                return "login";
            }else {
                model.addAttribute("message","密码更新错误");
                return "error";
            }
        }else {
            //校验失败
            model.addAttribute("message","用户名或邮箱错误");
            return "error";
        }
    }
}

